﻿using System.ComponentModel.DataAnnotations.Schema;

namespace Gardener.Iot.Impl.Entities
{
    /// <summary>
    /// 产品
    /// </summary>
    [Table("Iot" + nameof(Product))]
    public class Product : ProductDto, IEntityBase<MasterDbContextLocator, GardenerMultiTenantDbContextLocator>, IEntityTypeBuilder<Product, MasterDbContextLocator, GardenerMultiTenantDbContextLocator>
    {
        /// <summary>
        /// 设备集
        /// </summary>
        public ICollection<Device>? Devices { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Configure(EntityTypeBuilder<Product> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasIndex(x => x.ProductName).IsUnique();

            entityBuilder
           .HasMany(x => x.Devices)
           .WithOne(x => x.Product)
           .HasForeignKey(x => x.ProductId)
           .OnDelete(DeleteBehavior.ClientSetNull);
        }
    }
}
